package com.zxm.sunshineaiary.animation;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

import com.zxm.sunshineaiary.util.CharsUtil;

public class MonthLayoutAnimation extends Animation {

	private String TAG = "MonthLayoutAnimation";

	public static final int MARGIN_SHORT = 55;
	public static final int MARGIN_LONG = 320;

	private final int ANIMATION_TIME = 600;
	private int userAnimationTime = ANIMATION_TIME;
	private Context mContent;
	private View[] mViews;

	private int[] mLeftMargins;
	private int leftMarginDistance;
	private boolean mOpenMonehState;

	public MonthLayoutAnimation(Context context, View[] views, int margin) {
		this.mContent = context;
		this.leftMarginDistance = CharsUtil.dipTopx(mContent, margin);
		this.mViews = views;
		this.mLeftMargins = new int[views.length];

	}

	public void setAnimationTime(int time) {
		this.userAnimationTime = time;
	}

	@Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {
		Log.v(TAG, "initialize");
		super.initialize(width, height, parentWidth, parentHeight);

		setDuration(userAnimationTime);
		Interpolator mDecelerateInterpolater = AnimationUtils.loadInterpolator(
				mContent, android.R.anim.decelerate_interpolator);
		setInterpolator(mDecelerateInterpolater);
		initializeViewLeftMargin(mViews, mLeftMargins);

	}

	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		Log.v(TAG, "applyTransformation");
		int lenght = mLeftMargins.length;

		if (mOpenMonehState) {
			for (int i = 0; i < lenght; i++) {
				setAnimation(mViews[i], mLeftMargins[i], leftMarginDistance,
						-interpolatedTime);
			}
		} else {
			for (int i = 0; i < lenght; i++) {
				setAnimation(mViews[i], mLeftMargins[i], leftMarginDistance,
						interpolatedTime);
			}
		}
	}

	/**
	 * 初始化所有View的LeftMargin
	 * 
	 * @param vs
	 *            View数组
	 * @param margins
	 *            存放View LeftMargin的数组
	 */
	private void initializeViewLeftMargin(View[] vs, int[] margins) {
		for (int i = 0; i < vs.length; i++) {
			margins[i] = getViewStartLeftMargin(vs[i]);
			Log.v(TAG, margins[i] + "");
		}
	}

	/**
	 * 获得View的LeftMargin
	 * 
	 * @param view
	 * @return
	 */
	private int getViewStartLeftMargin(View view) {
		FrameLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
		int leftMargin = lp.leftMargin;
		return leftMargin;
	}

	/**
	 * 设置动画效果
	 * 
	 * @param v
	 * @param leftMargin
	 * @param interpolatedTime
	 */
	private void setAnimation(View v, int leftMargin, int endlefMargin,
			float interpolatedTime) {

		FrameLayout.LayoutParams params = (LayoutParams) v.getLayoutParams();
		params.leftMargin = (leftMargin)
				+ (int) (endlefMargin * interpolatedTime);
		v.setLayoutParams(params);
		Log.v(TAG, "v.setLayoutParams===" + getViewStartLeftMargin(v));
	}

	public void setOpenAnimationState(boolean mOpenMonehState) {
		this.mOpenMonehState = mOpenMonehState;
	}
}
